package org.springblade.modules.opcua.init;

import lombok.extern.slf4j.Slf4j;
import org.springblade.modules.opcua.action.OpcDataSaveFactory;
import org.springblade.modules.opcua.action.OpcJhWorker;
import org.springblade.modules.opcua.model.OpcTargetBean;
import org.springblade.modules.opcua.service.OpcTargetService;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class OpcJhRunner implements ApplicationRunner {
    //作业
    private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(50);
    @Resource
    private OpcTargetService configService;

    @Resource
    private OpcDataSaveFactory opcDataSaveFactory;

    @Override
    public void run(ApplicationArguments args) {
        List<OpcTargetBean> configBeanList = configService.findListBySpec(new OpcTargetBean());

        for (OpcTargetBean configBean : configBeanList) {
            //保存数据
            scheduledExecutorService.scheduleWithFixedDelay(new OpcJhWorker(configBean, opcDataSaveFactory), 5, configBean.getIntervalTime(), TimeUnit.SECONDS);
        }
    }


}
